﻿package mortal.game.proxy
{
    import Framework.Util.*;
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import com.gengine.debug.*;
    import extend.language.*;
    import mortal.common.error.*;
    import mortal.game.cache.*;
    import mortal.game.cache.packCache.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.pack.other.*;
    import mortal.mvc.core.*;

    public class PackProxy extends Proxy
    {
        private var _packCache:BackPackCache;
        public const posType:int = 0;
        private var _moneyCardData:ItemData;

        public function PackProxy()
        {
            this.init();
            return;
        }// end function

        private function init() : void
        {
            this._packCache = this.cache.pack.backPackCache;
            return;
        }// end function

        public function getBag(param1:int) : void
        {
            var _loc_2:* = EPlayerItemPosType.convert(param1);
            rmi.iBagPrx.get_async(new AMI_IBag_get(), _loc_2);
            return;
        }// end function

        public function tidy(param1:int) : void
        {
            var _loc_2:* = EPlayerItemPosType.convert(param1);
            rmi.iBagPrx.tidy_async(new AMI_IBag_tidy(this.tidySuccess, this.tidyFail, param1), _loc_2);
            return;
        }// end function

        private function tidySuccess(param1:AMI_IBag_tidy, param2:Boolean) : void
        {
            var _loc_3:PackPosTypeCache = null;
            Log.debug("tidySuccess", param2);
            if (!param2)
            {
                _loc_3 = Cache.instance.pack.getPackChacheByPosType(param1.userObject as int);
                _loc_3.sortItems();
                switch(param1.userObject)
                {
                    case EPlayerItemPosType._EPlayerItemPosTypeBag:
                    {
                        NetDispatcher.dispatchCmd(ServerCommand.BackPackItemsChange, null);
                        break;
                    }
                    case EPlayerItemPosType._EPlayerItemPosTypeWarehouse:
                    {
                        NetDispatcher.dispatchCmd(ServerCommand.UpdateWarehouse, null);
                        break;
                    }
                    case EPlayerItemPosType._EPlayerItemPosLottery:
                    {
                        NetDispatcher.dispatchCmd(ServerCommand.LotteryDrawBagInfoUpdate, null);
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            return;
        }// end function

        private function tidyFail(param1:Exception) : void
        {
            Log.debug("tidyFaile");
            return;
        }// end function

        public function useItem(param1:String, param2:int, param3:int = 1, param4:Array = null) : void
        {
            var _loc_5:Object = {uid:param1, code:param2, amount:param3, valuses:param4 ? (param4) : ([])};
            rmi.iBagPrx.use_async(new AMI_IBag_use(null, null, _loc_5), param1, param3, param4 ? (param4) : (new Array()));
            return;
        }// end function

        private function useSuccesss(param1:AMI_IBag_use) : void
        {
            var _loc_2:* = param1.userObject;
            return;
        }// end function

        private function useFail(param1:Exception) : void
        {
            return;
        }// end function

        public function split(param1:String, param2:int) : void
        {
            var _loc_3:Object = {uid:param1, amount:param2};
            rmi.iBagPrx.split_async(new AMI_IBag_split(this.splitSuccess, null, _loc_3), param1, param2);
            return;
        }// end function

        private function splitSuccess(param1:AMI_IBag_split) : void
        {
            var _loc_2:* = param1.userObject;
            return;
        }// end function

        public function destroy(param1:Array) : void
        {
            rmi.iBagPrx.destroy_async(new AMI_IBag_destroy(this.destroySuccess, this.destroyFail), param1);
            return;
        }// end function

        private function destroySuccess(param1:AMI_IBag_destroy) : void
        {
            return;
        }// end function

        private function destroyFail(param1:Exception) : void
        {
            MsgManager.showRollTipsMsg(ErrorCode.getErrorStringByCode(param1.code));
            return;
        }// end function

        public function sell(param1:Array, param2:Function = null) : void
        {
            rmi.iBagPrx.sell_async(new AMI_IBag_sell(this.sellSuccess, this.sellFail, param2), param1);
            return;
        }// end function

        private function sellFail(param1) : void
        {
            MsgManager.showRollTipsMsg(ErrorCode.getErrorStringByCode(param1.code));
            return;
        }// end function

        private function sellSuccess(param1:AMI_IBag_sell) : void
        {
            var _loc_2:* = param1.userObject as Function;
            if (_loc_2 != null)
            {
                _loc_2.apply();
            }
            return;
        }// end function

        public function moneyCardRequest(param1:ItemData) : void
        {
            if (!this._moneyCardData)
            {
                this._moneyCardData = param1;
                rmi.iBagPrx.getMoneyCardStat_async(new AMI_IBag_getMoneyCardStat(this.moneyCardCallBack));
            }
            return;
        }// end function

        private function moneyCardCallBack(param1:AMI_IBag_getMoneyCardStat, param2:int) : void
        {
            if (param2 == 0)
            {
                MoneyCardPackageWin.instance.show();
                MoneyCardPackageWin.instance.itemData = this._moneyCardData;
            }
            else if (param2 == 1)
            {
                MsgManager.showRollTipsMsg(Language.getString(20588));
            }
            this._moneyCardData = null;
            return;
        }// end function

        public function openTime(param1:int = 0, param2:Boolean = false) : void
        {
            var _loc_3:* = EPlayerItemPosType.convert(param1);
            rmi.iBagPrx.openTime_async(new AMI_IBag_openTime(this.getTimeSuccess, this.getTimeFail, param2), _loc_3);
            return;
        }// end function

        private function getTimeSuccess(param1:AMI_IBag_openTime, param2:int, param3:int) : void
        {
            var _loc_5:CDData = null;
            var _loc_6:int = 0;
            this._packCache.unLockTime = param2 + param3 + 1;
            var _loc_4:* = this._packCache.currentGrid;
            if (_loc_4 <= this._packCache.totalGride)
            {
                _loc_5 = Cache.instance.cd.registerCDData(CDDataType.backPackLock, "lockCD", _loc_5) as CDData;
                _loc_5.beginTime = FrameManager.flashFrame.getTime() - this._packCache.countdownTime * 1000;
                _loc_6 = _loc_4 == this._packCache.gridVec[0].grid + this._packCache.vipDrid ? (0) : (this._packCache.gridVec[_loc_4 - this._packCache.gridVec[0].grid - 1 - this._packCache.vipDrid].needTime);
                _loc_5.totalTime = (this._packCache.gridVec[_loc_4 - this._packCache.gridVec[0].grid - this._packCache.vipDrid].needTime - _loc_6) * 1000;
                _loc_5.startCoolDown();
            }
            if (param1.userObject)
            {
                NetDispatcher.dispatchCmd(ServerCommand.BackPackScrollToIndex, _loc_4);
            }
            else
            {
                NetDispatcher.dispatchCmd(ServerCommand.BackPackTimeGet, null);
            }
            return;
        }// end function

        private function getTimeFail(param1) : void
        {
            NetDispatcher.dispatchCmd(ServerCommand.BackPackTimeGet, null);
            return;
        }// end function

        public function openGrid(param1:int, param2:int, param3:int) : void
        {
            var _loc_4:* = EPlayerItemPosType.convert(param1);
            var _loc_5:* = new Object();
            _loc_5.type = param1;
            _loc_5.amount = param2;
            rmi.iBagPrx.open_async(new AMI_IBag_open(this.openSuccess, this.openFail, _loc_5), _loc_4, param2, param3);
            return;
        }// end function

        private function openSuccess(param1:AMI_IBag_open) : void
        {
            var _loc_2:* = param1.userObject;
            var _loc_3:* = _loc_2.type;
            var _loc_4:* = _loc_2.amount;
            switch(_loc_3)
            {
                case EPlayerItemPosType._EPlayerItemPosTypeBag:
                {
                    this._packCache.capacity = this._packCache.capacity + _loc_4;
                    NetDispatcher.dispatchCmd(ServerCommand.UpdateCapacity, null);
                    this.openTime(0, true);
                    break;
                }
                case EPlayerItemPosType._EPlayerItemPosTypeWarehouse:
                {
                    Cache.instance.pack.warehouseCache.capacity = Cache.instance.pack.warehouseCache.capacity + _loc_4;
                    NetDispatcher.dispatchCmd(ServerCommand.WarehouseGrid, null);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function openFail(param1:Exception) : void
        {
            MsgManager.showRollTipsMsg(ErrorCode.getErrorStringByCode(param1.code));
            return;
        }// end function

        public function move(param1:int, param2:Array) : void
        {
            rmi.iBagPrx.move_async(new AMI_IBag_move(this.moveSuccess, this.moveFail), EMoveType.convert(param1), param2);
            return;
        }// end function

        private function moveSuccess(param1:AMI_IBag_move) : void
        {
            return;
        }// end function

        private function moveFail(param1:Exception) : void
        {
            MsgManager.showRollTipsMsg(ErrorCode.getErrorStringByCode(param1.code));
            return;
        }// end function

    }
}
